package com.ego.item.service.impl;

import com.ego.common.utils.PageResult;
import com.ego.item.mapper.BrandMapper;
import com.ego.item.pojo.Brand;
import com.ego.item.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {

    @Autowired(required = false)
    private BrandMapper brandMapper;


    @Override
    public PageResult<Brand> getPageResult(Integer pageNo, Integer pageSize, String sortBy, Boolean descending, String key) {
        PageHelper.startPage(pageNo, pageSize);

        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().andLike("name", "%" + key + "%").orEqualTo("letter", key.toUpperCase());
        }
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + " " + (descending ? "desc" : "asc"));
        }
        List<Brand> list = brandMapper.selectByExample(example);
        PageInfo<Brand> pageInfo = new PageInfo<>(list);
        PageResult<Brand> pageResult = new PageResult<>();
        pageResult.setItems(pageInfo.getList());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setTotalPage(Long.valueOf(pageInfo.getPages()));
        return pageResult;
    }

    @Override
    public void addBrand(Brand brand, List<Long> cids) {
        brandMapper.insertSelective(brand);//保存一个实体，null的属性不会保存，会使用数据库默认值
        if (cids != null && cids.size() != 0) {
            for (Long cid :
                    cids) {
                //添加操作各个品牌和目录之间的关系, 有张中间表
                this.brandMapper.insertBrandCategory(cid, brand.getId());
            }
        }
    }

    @Override
    public void update(Brand brand, List<Long> cids) {
        brandMapper.updateByPrimaryKeySelective(brand);
        if (cids != null && cids.size() != 0) {
            //先删除之前中间表的记录
            brandMapper.deleteBrandCategory(brand.getId());
            //然后重新插入一下两者之间的关系
            for (Long cid :
                    cids) {
                this.brandMapper.insertBrandCategory(cid, brand.getId());
            }
        }
    }

    @Override
    public void deleteById(Long id) {

        //删除一下 和 类目之间的关系
        brandMapper.deleteBrandCategory(id);
        //然后删除自己
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> getListByCid(Long cid) {
        return brandMapper.getListByCid(cid);
    }

    @Override
    public Brand queryBrandById(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Brand> queryBrandListByBrandIds(List<Long> ids) {
        return brandMapper.selectByIdList(ids);
    }

}
